package configclient;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.json.simple.JSONObject;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
@RestController
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;
    @Value("${name}")
    String name;
    @Value("${config}")
    String config;
    
    @RequestMapping(value = "/getconfig")
    public List<Map> hi(@RequestParam String sourceType){
    	System.out.println(config);
    	ObjectMapper mapper = new ObjectMapper();  
    	JSONObject jsonObject = new JSONObject();
		try {
			jsonObject=mapper.readValue(config, JSONObject.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Map> list = (List) jsonObject.get(sourceType);
        return list;
    }
    
    @RequestMapping(value = "/getname")
    public String name(){
    	System.out.println(name);
    	return name;
    }
}
